package demo1;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-09-10
 * Time: 22:23
 */

class Sum{
    public static int n = 0;
    public static Object object = new Object();
    public static Object object2 = new Object();

    public void func1(){
        synchronized (this){
            for (int i = 0; i < 1000; i++) {
                n++;
            }
        }
//        for (int i = 0; i < 1000; i++) {
//            n++;
//        }
    }

    public synchronized void func2(){
//        synchronized (object2){
//            for (int i = 0; i < 1000; i++) {
//                n++;
//            }
//        }
        for (int i = 0; i < 1000; i++) {
            n++;
        }
    }


}

public class Test4 {
    public static int n = 0;
    public static Object object = new Object();
    public static Object object2 = new Object();
    public static void func1(){
//        synchronized (object){
//            for (int i = 0; i < 1000; i++) {
//                n++;//
//            }
//        }
        for (int i = 0; i < 1000; i++) {
            n++;
        }
    }

    public static void func2(){
//        synchronized (object2){
//            for (int i = 0; i < 1000; i++) {
//                n++;
//            }
//        }
        for (int i = 0; i < 1000; i++) {
            n++;
        }
    }

    public synchronized void func3(){
        ;
    }

    public static void main(String[] args) throws InterruptedException {
        Sum sum = new Sum();
        Thread t1 = new Thread(() -> {
            sum.func1();
        });
        Thread t2 = new Thread(() -> {
            sum.func2();
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(Sum.n);
    }

}
